package com.qfedu.fmmall.service.impl;

import com.qfedu.fmmal.vo.ResStatus;
import com.qfedu.fmmal.vo.ResultVO;
import com.qfedu.fmmall.dao.ShoppingCartMapper;
import com.qfedu.fmmall.entity.ShoppingCart;
import com.qfedu.fmmall.entity.VO.ShoppingCartVO;
import com.qfedu.fmmall.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public ResultVO addShoppingCart(ShoppingCart cart) {
        cart.setCartTime(sdf.format(new Date()));
        int i = shoppingCartMapper.insert(cart);
        if(i>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }else {
            return new ResultVO(ResStatus.NO,"fail",null);
        }

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public ResultVO listShoppingCartsByUserId(int userId) {
        List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByUserId(userId);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", list);
        return resultVO;
    }

    @Override
    public ResultVO updateCartNum(int cartId, int cartNum) {
        int i = shoppingCartMapper.updateCartnumByCartId(cartId, cartNum);
        if (i>0){
            return new ResultVO(ResStatus.OK,"update success",null);
        }else {
            return new ResultVO(ResStatus.NO,"update fail",null);
        }

    }

    @Override
    public ResultVO listShoppingCartsByCids(String cids) {
        //使用tkMapper查询出来的购物车中没有商品图片、商品名称，单价等信息，不满足需求。
//        Example example = new Example(ShoppingCart.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andIn("cartId",cids);
//
//        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByExample(example);

        String[] arr = cids.split(",");
        List<Integer> cartIds = new ArrayList<>();
        for (int i = 0; i<arr.length;i++){
            cartIds.add(Integer.parseInt(arr[i]));
        }


        List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByCids(cartIds);
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", list);

        return resultVO;
    }
}
